package com.javen.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.zimmur.tools.HttpClientUtil;
import com.zimmur.tools.MD5Util;

public class TopicRunnable extends Thread {
	private  CountDownLatch countDown;
	private  CyclicBarrier cyclicBarrier;
	private static String  local_token="QQdmO9M9qOQJWBkr3_Al5O-3R9PFB3nKYqliOLLv7Es";
	private static String  local_key="193e09e1e9ae46f9";
	//private static String url="http://192.168.0.160";
	private final static String url="https://service.popolook.com:8443";
	String token="W2T4fWGCTBYB326t1BJVye-3R9PFB3nKYqliOLLv7Es";
	String user_token="zeeSFgdEGdnjlTjdBfCe-VZvcjRn9sGdmSTO6hBhToc";
	String key="5aaf9eee013d4468";
	private static AtomicInteger nums=new AtomicInteger(0);
	private static Long average=0L;
	private int num;
	public TopicRunnable(String name,CountDownLatch countDown,CyclicBarrier cyclicBarrier,int num){
		this.countDown = countDown;
		this.cyclicBarrier=cyclicBarrier;
		this.num=num;
		super.setName(name);
		
	}
	public void run() {
		String mobile=null;
		if(num<10){
			mobile="1526666660"+this.num;
		}else{
			mobile="152666666"+this.num;
		}
		
		System.out.println("no:"+this.getName()+"_mobile:"+mobile);
		try {
			countDown.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		long start_time = System.currentTimeMillis();
		System.out.println("no:"+this.getName()+"_start"+start_time);
//		topic_topicindex("/appserver/restful/topic/topicindex", local_token,local_key);
		//send_coupon("/appserver/restful/test", local_token,local_key,mobile);
		testGETSendMsg("/restful/maker/index", token,key);
		long end_time = System.currentTimeMillis();
		long do_time=end_time-start_time;
		System.out.println("no:"+this.getName()+"_do_time:"+do_time);
//		synchronized (average) {
//			average+=do_time;
//		}
		//已完成
		try {
			cyclicBarrier.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		//System.out.println("全部执行完成: 平均耗时="+average/100);
//		countDown1.countDown();
//		try {
//			countDown1.await();
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("no:"+i+"_average:"+average/100);
	   
		
	}
	private static void topic_topicindex(String uri,String token,String key){
		long timeunix = System.currentTimeMillis();
		try {
			List<NameValuePair> param=new ArrayList<>();
			param.add(new BasicNameValuePair("token", token));
			String content = "GET"+uri+timeunix+token+key+"0";
			//System.out.println(content);
			List<NameValuePair> headers = new ArrayList<NameValuePair>();
			headers.add(new BasicNameValuePair("timestamp", timeunix+""));
			headers.add(new BasicNameValuePair("body_length", "0"));
			headers.add(new BasicNameValuePair("sign", 3614+":"+MD5Util.MD5Encode(content, "utf-8").toUpperCase()));
			String strRet = HttpClientUtil.doGet(url+uri, param, headers,50000);
			//System.out.println(strRet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void send_coupon(String uri,String token,String key,String mobileNo){
		long timeunix = System.currentTimeMillis();
		try {
			List<NameValuePair> param=new ArrayList<>();
			param.add(new BasicNameValuePair("token", token));
			param.add(new BasicNameValuePair("mobileNo", mobileNo));
			String content = "GET"+uri+timeunix+token+key+"0";
			//System.out.println(content);
			List<NameValuePair> headers = new ArrayList<NameValuePair>();
			headers.add(new BasicNameValuePair("timestamp", timeunix+""));
			headers.add(new BasicNameValuePair("body_length", "0"));
			headers.add(new BasicNameValuePair("sign", 3614+":"+MD5Util.MD5Encode(content, "utf-8").toUpperCase()));
			String strRet = HttpClientUtil.doGet(url+uri, param, headers,50000);
			//System.out.println(strRet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void testGETSendMsg(String uri,String token,String key){
		long timeunix = System.currentTimeMillis();      
        try {
        	List<NameValuePair> param=new ArrayList<>();
        	param.add(new BasicNameValuePair("token", token));
        	param.add(new BasicNameValuePair("pageNow", 1+""));
        	String content = "GET"+uri+timeunix+token+key+"0";
        	List<NameValuePair> headers = new ArrayList<NameValuePair>();
        	headers.add(new BasicNameValuePair("timestamp", timeunix+""));
        	headers.add(new BasicNameValuePair("body_length", "0"));
        	headers.add(new BasicNameValuePair("sign", 3614+":"+MD5Util.MD5Encode(content, "utf-8").toUpperCase()));
			String strRet = HttpClientUtil.doGetSSL(url+uri, param, headers);
			//String strRet = HttpClientUtil.doGet(url+uri, param, headers,5000);
			//System.out.println(strRet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		long start_time = System.currentTimeMillis();
		System.out.println("start"+start_time);
		//topic_topicindex("/appserver/restful/topic/topicindex", local_token,local_key);
		send_coupon("/appserver/restful/test", local_token,local_key,"15200000010");
		long end_time = System.currentTimeMillis();
		long do_time=end_time-start_time;
		System.out.println("do_time:"+do_time);
	}
} 


